<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'header.php'?>
	<div class="page-header">
		<h1><?=isset($coachInfo) ? '编辑执教关系' : '新增执教关系'?></h1>
	</div>

	<div class="col-xs-12">
		<form class="form-horizontal" id="coach-form">
            <input type="hidden" name="id" value="<?=isset($coachInfo) ? $coachInfo['id'] : '' ?>" />
            <input type="hidden" name="teacher_id" value="<?=isset($coachInfo['teacher_id']) ? $coachInfo['teacher_id'] : '' ?>" />
            <input type="hidden" name="class_ids" value="<?=isset($coachInfo['class_ids']) ? $coachInfo['class_ids'] : '' ?>" />
            <div class="form-group">
                <label class="col-sm-3 control-label no-padding-right">教师名称：</label>
                <div class="col-sm-6">
                    <span class="block input-icon input-icon-right pd0">
                        <input class="width-100" type="text" placeholder="请选择教师" id="select-master" value="<?= isset($coachInfo) ? $coachInfo['teacher_realname'] : '' ?>" />
                        <i class="ace-icon fa fa-user"></i>
                    </span>
                </div>
            </div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">执教角色：</label>
				<div class="col-sm-6">
					<select class="form-control" name="coach_type">
                        <?php foreach($coachTypeArr as $coachType): ?>
                            <option value="<?= $coachType['value'] ?>" <?= isset($coachInfo) && $coachInfo['coach_type'] == $coachType['value'] ? 'selected' : '' ?>><?= $coachType['title'] ?></option>
                        <?php endforeach ?>
					</select>
				</div>
            </div>

			<div class="form-group div-class" <?= !isset($coachInfo) || (isset($coachInfo) && ($coachInfo['coach_type'] == COACH_TYPE_TEACHER || $coachInfo['coach_type'] == COACH_TYPE_MASTER)) ? '' : 'style="display:none;"' ?>>
                <label class="col-sm-3 control-label no-padding-right">执教班级：</label>
                <div class="col-sm-6">
                    <span class="block input-icon input-icon-right pd0">
                        <input class="width-100" type="text" placeholder="请选择班级" id="select-class" value="<?= isset($coachInfo) ? $coachInfo['class_name'] : '' ?>" />
                        <i class="ace-icon fa fa-graduation-cap""></i>
                    </span>
                </div>
			</div>
			<div class="form-group div-grade" <?= isset($coachInfo) && $coachInfo['coach_type'] == COACH_TYPE_GRADE_ADMIN ? '' : 'style="display:none;"' ?>>
                <label class="col-sm-3 control-label no-padding-right">管理年级：</label>
                <div class="col-sm-6">
					<select class="form-control" name="grade_id" data-placeholder="选择年级">
                        <?php foreach($gradeArr as $grade): ?>
                            <option value="<?= $grade['id'] ?>" <?= isset($coachInfo) && $coachInfo['grade_id'] == $grade['id'] ? 'selected' : '' ?>><?= $grade['grade_name'] ?></option>
                        <?php endforeach ?>
                    </select>
                </div>
            </div>
			<div class="form-group div-subject"  <?= isset($coachInfo) && $coachInfo['coach_type'] == COACH_TYPE_TEACHER ? '' : 'style="visibility:hidden"' ?>>
                <label class="col-sm-3 control-label no-padding-right">执教科目：</label>
                <div class="col-sm-6">
                    <select class="chosen-select form-control tag-input-style" name="subject_ids" data-placeholder="选择科目" multiple="multiple">
                        <?php foreach($subjectArr as $subject): ?>
                            <option value="<?= $subject['id'] ?>" <?= isset($coachInfo) && in_array($subject['id'], $coachInfo['subjectIdArr']) ? 'selected' : '' ?>><?= $subject['lesson_name'] ?></option>
                        <?php endforeach ?>
                    </select>
                </div>
            </div>

			<div class="clearfix">
				<div class="col-md-offset-3 col-md-9">
					<button class="btn btn-info" type="submit">
						<i class="ace-icon fa fa-check"></i> 提交
					</button>
				</div>
			</div>
		</form>
	</div>
<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'footer.php'?>

<script type="text/javascript">
	$(function(){
		if($("#select-class").val().length > 60) {
			var names = $("#select-class").val();
			names = names.substring(0,60) + '...';
			$("#select-class").val(names);
		}

		// // 初始化选择老师控件
		// $("#select-master").selectTeacher({
		// 	"callback": function(result) {
		// 		for(var i in result) {
		// 			$("[name=teacher_id]").val(i);
		// 			$("#select-master").val(result[i].name);
		// 		}
		// 	}
		// });

		
		// 初始化选择老师控件
		$("#select-master").selectTeacher({
			"resultDom": $("[name=teacher_id]"),
		});

		// 初始化选择班级控件
		$("#select-class").selectSourceGrade({
			"title": '选择班级',
			"multiSelect": true,
			"modalDataUrl": '<?= admin_url('school/select_grade_plugin_data_source') ?>',
			"resultDom": $("[name=class_ids]"),
			"callback": function(result) {
				// console.log(result);
				var idArr = [];
				var nameArr = [];
				for(var i in result) {
					idArr.push(i);
					nameArr.push(result[i].name);
				}
				$("[name=class_ids]").val(idArr.join(','));
				var names = nameArr.join('，');
				if(names.length > 60) {
					names = names.substring(0,60) + '...';
				}
				$("#select-class").val(names);
			}
		});
		// 下拉选择
		$('.chosen-select').chosen({
			search_contains: true
		});
        // 监听执教角色变化，隐藏和显示执教科目选框
        $("[name=coach_type]").on("change", function () {
            var coachType = $(this).val();
            if(coachType == "<?= COACH_TYPE_TEACHER ?>") {
				// 任课教师
				$(".div-class").css("display", "block");
				$(".div-grade").css("display", "none");
                $(".div-subject").css("visibility", "visible");
            } else if(coachType == "<?= COACH_TYPE_MASTER ?>") {
				// 班主任
				$(".div-class").css("display", "block");
				$(".div-grade").css("display", "none");
                $(".div-subject").css("visibility", "hidden");
			} else if(coachType == "<?= COACH_TYPE_GRADE_ADMIN ?>") {
				// 年级管理员
				$(".div-class").css("display", "none");
				$(".div-grade").css("display", "block");
                $(".div-subject").css("visibility", "hidden");
            } else if(coachType == "<?= COACH_TYPE_SCHOOL_ADMIN ?>") {
				// 年级管理员
				$(".div-class").css("display", "none");
				$(".div-grade").css("display", "none");
                $(".div-subject").css("visibility", "hidden");
            }
        });
    
		// 提交表单
		var form = $("#coach-form");
		var submitUrl = "<?=admin_url('staff/save_coach_action')?>";
		form_submit(form, submitUrl, function(res) {
			tips_alert(res.msg, res.success, function(){
				if(res.success) {
					window.location.href = "<?=admin_url('staff/coach')?>";
				}
			});
		});
	})
</script>